<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Business\Business;
use Illuminate\Support\Facades\View;

class BusinessController extends Controller
{
    public function index(Request $request)
    {
        return view('business.index');
    }

    // 获取用户男女比例
    public function gender(Request $request)
    {
        // 查询客户总数
        $count = Business::count();

        $list = Business::groupby('gender')->where('gender', '!=', '0')->selectRaw('count(id) as total, gender')->get();

        // 遍历计算
        $list = collect($list)->toArray();

        $res = [];
        foreach($list as $item)
        {
            
            $res[] = [
                'total' => bcdiv(bcmul($item['total'], 100), $count, 1)
            ];
        }

        return $res;
    }

    // 获取用户认证
    public function auth(Request $request)
    {
        $list = Business::groupby('auth')->selectRaw('count(id) as total, auth')->get();

        $list = collect($list)->toArray();

        $res = [];
        foreach($list as $item)
        {
            $res[] = [
                'total' => $item['total'],
                'name' => $item['auth'] == '0' ? '未认证' : '已认证'
            ];
        }

        return $res;
    }

    // 获取用户地区分布数据（绘制地区客户占比饼图）
    public function region(Request $request)
    {
        $list = Business::from('business as bus')
            ->groupBy('bus.city')
            ->whereNotNull('bus.city')
            ->leftJoin('region', 'bus.city', '=', 'region.code')
            ->selectRaw('count(id) as total, fa_bus.city, fa_region.name')
            ->get();

        // var_dump(collect($list)->toArray());
        // exit;

        // 返回请求数据
        return $list->toJson();
    }

    // 平台来源分布
    public function source(Request $request)
    {
        $list = Business::from('business as bus')
            ->groupby('bus.sourceid')
            ->whereNotNull('bus.sourceid')
            ->leftjoin('business_source as source', 'bus.sourceid', '=', 'source.id')
            ->selectRaw('count(fa_bus.id) as total, fa_bus.sourceid, fa_source.name')
            ->get();

        return $list->toJson();
    }


    public function demo(Request $request)
    {

        // 获取get请求数据
        $name = $request->input('name');
        // var_dump($name);

        // 获取post请求数据
        $nickname = $request->nickname;
        $password = $request->password;

        var_dump($nickname);
        var_dump($password);


        // $business = DB::select('SELECT * FROM fa_business');
        // $business = DB::table('business')->get();
        // $business = Business::all();   // 使用模型查询所有

        // 查询数据库(带条件查询 ? 绑定参数)
        // $business = DB::select('SELECT * FROM `fa_business` WHERE `auth` = ?', ['1']);
        // $business = DB::table('business')->where('auth', '1')->get();
        $business = Business::where('auth', '1')->orderby('createtime', 'desc')->get();

        // 使用命名绑定查询
        // $business = DB::select('SELECT * FROM fa_business WHERE id = :id', ['id' => 60]);

        // first只获取一行数据 value获取单个值
        // $business = DB::table('business')->where('auth', '1')->first();

        $one = Business::find(60);  // 只查找ID为60的一条

        // 返回到页面上
        // return view('businessindex', ['business' => $business]);
        return View::make('businessindex', ['business' => $business]);
    }
}
